let requestUrl;

function addHandler() {
	requestUrl = "/equipment_view_attribute/insert";

	$("#view_attribute_dialog").dialog("open");
}

function editHandler() {
	let data = $("#view_attribute_list").datagrid("getSelected");

	if(data) {
		requestUrl = "/equipment_view_attribute/updateById";

		$("#id").val(data.id);
		$("#min").numberspinner("setValue", data.min);
		$("#max").numberspinner("setValue", data.max);
		$("#grade").numberspinner("setValue", data.grade);
		$("#forgeType").combobox("setValue", data.forgeType);
		$("#propertyId").combobox("setValue", data.propertyId);
		$("#equipmentId").combobox("setValue", data.equipmentId);

		$("#view_attribute_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

$(document).ready(function() {

	fetchSelect("#forgeType", "/forge_type/selectAll", {
		width: 150,
		required: true
	});

	$("#grade").numberspinner({
		min: 10,
		width: 150,
		increment: 10,
		required: true
	});

	$("#min").numberspinner({
		min: 1,
		width: 150,
		required: true
	});

	$("#max").numberspinner({
		min: 1,
		width: 150,
		required: true
	});

	fetchSelect("#propertyId", "/property/selectAll", {
		width: 150,
		required: true
	});
	
	fetchSelect("#equipmentId", "/equipment_type/selectParent", {
		width: 150,
		required: true,
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#view_attribute_dialog").dialog({
		title: "装备预览属性",
		width: 500,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#view_attribute_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#view_attribute_dialog").dialog("close");
						$("#view_attribute_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#view_attribute_form").form("clear");
				$("#view_attribute_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_filter + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	let datagrid = $("#view_attribute_list").datagrid({
		url: "/equipment_view_attribute/selectByPage",
		method: "get",
		height: height_table,
		rownumbers: true,
		pagination: true,
		fitColumns: true,
		remoteFilter: true,
		clientPaging: false,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}],
		columns: [[
			{field: "id", title: "编号", hidden: true},
			{field: "grade", title: "等级", align: "center", width: 50,
				formatter: function(value) {
					return "<div>" + value + "级</div>";
				}
			},
			{field: "propertyId", title: "属性", align: "center", width: 50
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/property/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#property_" + rowIndex).html(data.name);
					}, error);

					return "<div class='blue' id='property_" + rowIndex + "' ></div>";
				}
			},
			{field: "min", title: "属性下限", align: "center", width: 50},
			{field: "max", title: "属性上限", align: "center", width: 50},
			{field: "forgeType", title: "打造类型", align: "center", width: 50
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/forge_type/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#forge_type_" + rowIndex).html(data.name);
					}, error);

					return "<div class='blue' id='forge_type_" + rowIndex + "' ></div>";
				}
			},
			{field: "equipmentId", title: "装备类型", align: "center", width: 50
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/equipment_type/selectTop", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#equipment_type_" + rowIndex).attr({
							"alt": data.name,
							"src": data.image,
							"title": data.name
						});
					}, error);

					return "<img height='" + size + "' id='equipment_type_" + rowIndex + "' />";
				}
			}
		]]
	});

	datagrid.datagrid("enableFilter", [{
		field: "equipmentId",
		type: "combobox",
		options: {
			url: "/equipment_type/selectParent",
			valueField: "id",
			textField: "name",
			panelHeight: "auto",
			formatter: function(row) {
				return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
			}
		},
		operator: ["equal"]
	}, {
		field: "grade",
		type: "numberspinner",
		options: {
			min: 0,
			max: 100
		},
		operator: ["equal", "notequal", "less", "lessorequal", "greater", "greaterorequal"]
	}, {
		field: "forgeType",
		type: "combobox",
		options: {
			url: "/forge_type/selectAll",
			valueField: "id",
			textField: "name",
			panelHeight: "auto"
		},
		operator: ["equal"]
	}, {
		field: "propertyId",
		type: "combobox",
		options: {
			url: "/property/selectAll",
			valueField: "id",
			textField: "name",
			panelHeight: "auto"
		},
		operator: ["equal"]
	}]);

});